package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.ProjectPlans;
import com.zzyl.exception.BaseException;
import com.zzyl.service.NursingService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.zzyl.enums.BasicEnum.PROJECT_DELETE_FAIL;


@RestController
@RequestMapping("/nursing_project")
public class NursingProjectController extends BaseController{

    @Autowired
    private NursingService nursingService;

    @GetMapping()
    public ResponseResult<PageResponse> list(
            @RequestParam(value ="name" ,required = false) String name,
            @RequestParam(value ="pageNum",  required = false) Integer pageNum,
            @RequestParam(value ="pageSize", required = false)Integer pageSize,
            @RequestParam(value ="status",required = false) Integer status) {
            PageResponse<NursingProject> PageNursingProject = nursingService.list(name, pageNum, pageSize, status);
            List<NursingProject> projectList = PageNursingProject.getRecords();
            List<NursingProjectVo> projectVoList = projectList.stream().map(project -> {
                NursingProjectVo projectVo = null;
                Integer i = nursingService.deleteBeforeSelect(project.getId());
                if(i>0){
                    projectVo = BeanUtil.toBean(project, NursingProjectVo.class);
                    projectVo.setCount(i);
                } else {
                    projectVo = BeanUtil.toBean(project, NursingProjectVo.class);
                }
                return projectVo;
            }).collect(Collectors.toList());
            PageResponse<NursingProjectVo> pageNursingProjectVo = new  PageResponse<NursingProjectVo>();
            pageNursingProjectVo.setRecords(projectVoList);
            pageNursingProjectVo.setTotal(PageNursingProject.getTotal());
            pageNursingProjectVo.setPages((long) PageNursingProject.getPages());
            pageNursingProjectVo.setPage(PageNursingProject.getPage());
            return ResponseResult.success(pageNursingProjectVo);
    }

    @PostMapping
    public ResponseResult add(@RequestBody NursingProject nursingProject) {
        nursingService.add(nursingProject);
        return ResponseResult.success();
    }

    @GetMapping("/{id}")
    public ResponseResult<NursingProject> getById(@PathVariable("id") Long id) {
        NursingProject nursingProject = nursingService.getById(id);
        return ResponseResult.success(nursingProject);
    }

    @PutMapping
    public ResponseResult updateById(@RequestBody NursingProject nursingProject) {
        nursingService.updateById(nursingProject);
        return ResponseResult.success();
    }

    @Transactional
    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable("id") Long id) {
        Integer cnt = nursingService.deleteBeforeSelect(id);
        if(cnt != 0){
            // return ResponseResult.error("项目已经被关联无法删除", null);
            throw new BaseException(PROJECT_DELETE_FAIL);
        }
        nursingService.deleteById(id);
        return ResponseResult.success();
    }

    @PutMapping("/{id}/status/{status}")
    public ResponseResult disableById(@PathVariable("id") Long id,
                                      @PathVariable("status") Integer status) {
        nursingService.disableById(id, status);
        return ResponseResult.success();
    }

    @GetMapping("/all")
    public ResponseResult<List<NursingProject>> listAll() {
        List<NursingProject> nursingProjectList = nursingService.listAll();
        return ResponseResult.success(nursingProjectList);
    }
}
